package com.skyjing.astools

//import com.skyjing.askit.dsl.seekbar.setOnSeekBarChangeListener

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkRequest
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.skyjing.askit.*
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity(R.layout.activity_main), View.OnClickListener {

    private var count = 0
    private var count1 = 0
    private var count2 = 0

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.tvTest->{
                AsToast.normal("123")
//            AsToast.normal("sdfsfd" , gravity = Gravity.TOP )
//            AsToast.normal("sdfs" , textColor = getColor(R.color.colorPrimary))
//            AsToast.info("sdfdsf")
//            AsToast.info("sdfdsf" , gravity = Gravity.CENTER , duration = 100)
//            AsToast.success("dsfsdf" , gravity = Gravity.CENTER)
//            AsImageTools.fastBlur(null , 2f , 1f )
                AsSpTool.put( key = "111" , value = "ssss")
                AsSpTool.put( tag = "testSp" , key = "222" , value = "aaa")
                LogUtils.e("sdfsd")
                Settings.System.putInt(this.contentResolver , Settings.System.WIFI_SLEEP_POLICY ,Settings.System.WIFI_SLEEP_POLICY_NEVER)
                LogUtils.e("电池",AsRequstWhiteList.isIgnoringBatteryOptimizations())
                AsRequstWhiteList.requestIgnoreBatteryOptimizations()
                LogUtils.e(AsSpTool.getString("www"))
            }
            R.id.btnDownload->{
//                AsToast.error("123")
                AsToast.normal(R.string.test_toast_tips)
//                AsToast.info("123")
                LogUtils.e("网络测试" , "${AsNetTool.isConnected(this)}")
                LogUtils.e("使用系统下载器java")
                LogUtils.e("保存数据" , "默认 ${AsSpTool.getString("111")}")
                LogUtils.e("保存数据" , "默认 ${AsSpTool.getString("333" , "9958")}")
                var url = "https://c9dec4e5c25870b8d86ff783ba4e7d2a.dd.cdntips.com/imtt.dd.qq.com/16891/apk/C10B2237586138DF3909E47B981B73F9.apk"
//                var url = "https://wesmart-app.oss-cn-hangzhou.aliyuncs.com/dev/dynamic/images/2020/06/29/fa19b62f9ac34711a4a93ca0aee4434d_3648*2736.png"
//                AsDownloadManagerUitls(this , url )
//                    .setListener(object : AsDownloadManagerUitls.DownloadManagerUitlsListener{
//                        override fun onSuccess(path: String?) {
//                            //onSuccess:path= /storage/emulated/0/Android/data/com.wesmart.mvvm/files/Download/C10B2237586138DF3909E47B981B73F9.apk
//                            LogUtils.e("DownloadManagerUitls", "onSuccess:path= $path")
//                        }
//
//                        override fun onFailed(throwable: Throwable?) {
//                            LogUtils.e("DownloadManagerUitls", "onFailed: ${throwable.toString()}")
//                        }
//
//                        override fun onDownloadPercent(totalSize: Long, currentSize: Long) {
//                            TODO("Not yet implemented")
//                        }
//
//                        override fun onPrepare() {
//                            LogUtils.e("DownloadManagerUitls", "onPrepare")
//                        }
//                    }).download()

//                com.skyjing.askitjava.AsDownloadManagerUitls(this , url , "9527.apk" ,
//                com.skyjing.askitjava.AsDownloadManagerUitls.SAVE_FILE_DCIM)
////                com.skyjing.askitjava.AsDownloadManagerUitls(this , url ,"/storage/emulated/0/测试/", "9527.apk")
//                    .setListener(
//                        object : com.skyjing.askitjava.AsDownloadManagerUitls.DownloadManagerUitlsListener{
//                            override fun onSuccess(path: String?) {
//                                LogUtils.e("DownloadManagerUitls", "onSuccess:path= $path")
//                            }
//
//                            override fun onFailed(throwable: Throwable?) {
//                                LogUtils.e("DownloadManagerUitls", "onFailed: ${throwable.toString()}")
//                            }
//
//                            override fun onDownloadPercent(totalSize: Long, currentSize: Long) {
//                                val pre = currentSize * 100f / totalSize
//                                runOnUiThread {
//                                    tvTest.text = "下载进度:${String.format("%.2f" , pre)}%"
//                                }
//                                LogUtils.e("DownloadManagerUitls", "下载进度:${String.format("%.2f" , pre)}%")
//                            }
//
//                            override fun onPrepare() {
//                                LogUtils.e("DownloadManagerUitls", "onPrepare")
//                            }
//
//                        }
//                    )
//                    .setTitle("9527")
//                    .setDescription("1111111111111111111111111111111111111111111111")
//                    .setRefreshTime(1000)
//                    .download()

//                com.skyjing.askitjava.AsDownloadManagerUitls(
//                    this, url, "9527.apk",
//                    com.skyjing.askit.java.AsDownloadManagerUitls.SAVE_FILE_DCIM
//                )
////                com.skyjing.askitjava.AsDownloadManagerUitls(this , url ,"/storage/emulated/0/测试/", "9527.apk")
//                    .setListener(object : com.skyjing.askitjava.AsDownloadManagerUitls.ListenerImpl(){
//                        override fun onSuccess(path: String?) {
//                                LogUtils.e("DownloadManagerUitls", "onSuccess:path= $path")
//                            }
//
//                            override fun onFailed(throwable: Throwable?) {
//                                LogUtils.e("DownloadManagerUitls", "onFailed: ${throwable.toString()}")
//                            }
//
//                            override fun onDownloadPercent(totalSize: Long, currentSize: Long) {
//                                val pre = currentSize * 100f / totalSize
//                                runOnUiThread {
//                                    tvTest.text = "下载进度:${String.format("%.2f" , pre)}%"
//                                }
//                                LogUtils.e("DownloadManagerUitls", "下载进度:${String.format("%.2f" , pre)}%")
//                            }
//
//                            override fun onPrepare() {
//                                LogUtils.e("DownloadManagerUitls", "onPrepare")
//                            }
//                    })
//                    .setTitle("9527")
//                    .setDescription("1111111111111111111111111111111111111111111111")
//                    .setRefreshTime(1000)
//                    .download()
//                AsClipboardTool.copyText(this , "屌你老母")
            }
            R.id.btnDownloadkt->{
                LogUtils.e("程序存活" , "${AsTool.isAppAlive(this , "com.wesmart.mvvm")}")
//                LogUtils.e("使用系统下载器 btnDownloadkt")
//                var url = "https://c9dec4e5c25870b8d86ff783ba4e7d2a.dd.cdntips.com/imtt.dd.qq.com/16891/apk/C10B2237586138DF3909E47B981B73F9.apk"
                var url = "https://wesmart-app.oss-cn-hangzhou.aliyuncs.com/dev/dynamic/images/2020/06/29/fa19b62f9ac34711a4a93ca0aee4434d_3648*2736.png"
//                AsDownloadManagerUitls(this , url , savePath = "/storage/emulated/0/下载测试/" , saveFileType = Environment.DIRECTORY_MOVIES)
//                AsDownloadManagerUitls(this , url ,  saveFileType = Environment.DIRECTORY_MOVIES)
//                    .setListener(object : AsDownloadManagerUitls.DownloadManagerUitlsListener{
//                        override fun onSuccess(path: String?) {
//                            //onSuccess:path= /storage/emulated/0/Android/data/com.wesmart.mvvm/files/Download/C10B2237586138DF3909E47B981B73F9.apk
//                            LogUtils.e("DownloadManagerUitls", "onSuccess:path= $path")
//                        }
//
//                        override fun onFailed(throwable: Throwable?) {
//                            LogUtils.e("DownloadManagerUitls", "onFailed: ${throwable.toString()}")
//                        }
//
//                        override fun onDownloadPercent(totalSize: Long, currentSize: Long) {
//                            val pre = currentSize * 100f / totalSize
//                            runOnUiThread {
//                                tvTest2.text = "下载进度:${String.format("%.2f" , pre)}%"
//                            }
//                            LogUtils.e("DownloadManagerUitls", "下载进度:${String.format("%.2f" , pre)}%")
//                        }
//
//                        override fun onPrepare() {
//                            LogUtils.e("DownloadManagerUitls", "onPrepare")
//                        }
//                    })
//                    .setTitle("9918")
//                    .setDescription("88888888888888888888888888888888")
//                    .setRefreshTime(1000L)
//                    .download()

//                AsDownloadManagerUitls(this , url ,  saveFileType = Environment.DIRECTORY_MOVIES)
//                    .setListener {
////                        this.percent { totalSize, currentSize ->
////                            val pre = currentSize * 100f / totalSize
////                            runOnUiThread {
////                                tvTest2.text = "下载进度:${String.format("%.2f" , pre)}%"
////                            }
////                            LogUtils.e("DownloadManagerUitls", "下载进度:${String.format("%.2f" , pre)}%")
////                        }
//                        this.fail {
//                            LogUtils.e("DownloadManagerUitls", "onFailed: ${it.toString()}")
//                        }
//                        success {
//                            LogUtils.e("DownloadManagerUitls", "onSuccess:path= $it")
//                        }
//                        prepare {
//                            LogUtils.e("DownloadManagerUitls", "onPrepare")
//                        }
//                    }.download()

                AsDownloadManagerUitls(
                    this,
                    url,
                    saveFileType = Environment.DIRECTORY_MOVIES
                )
                    .setListener(object : AsDownloadManagerUitls.ListenerImpl(){
                        override fun onPrepare() {
                            LogUtils.e("DownloadManagerUitls", "onPrepare")
                        }

                        override fun onSuccess(path: String?) {
                            LogUtils.e("DownloadManagerUitls", "onSuccess:path= $path")
                        }

                        override fun onFailed(throwable: Throwable?) {
                            LogUtils.e("DownloadManagerUitls", "onFailed: ${throwable.toString()}")
                        }

                        override fun onDownloadPercent(totalSize: Long, currentSize: Long) {
                            val pre = currentSize * 100f / totalSize
                            runOnUiThread {
                                tvTest2.text = "下载进度:${String.format("%.2f" , pre)}%"
                            }
                            LogUtils.e("DownloadManagerUitls", "下载进度:${String.format("%.2f" , pre)}%")
                        }
                    }).download()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
        AsSpTool.init()
        AsSpTool.init("testSp")
        tvTest.setOnClickListener(this)
        btnDownload.setOnClickListener(this)
        btnDownloadkt.setOnClickListener(this)
        Environment.getExternalStorageState()
        LogUtils.e("Environment", "${Environment.getExternalStorageState()}")
        LogUtils.e("Environment", "${Environment.getExternalStorageDirectory()}")
        LogUtils.e("Environment", "${this.getExternalFilesDir(null)}")
        val list = ArrayList<String>()
//        for (i in AsTool.getAppSignature()?.get(0))
        LogUtils.e("Signature", "${AsTool.getAppSignature()?.toList()?.toTypedArray()?.contentToString()}")
        LogUtils.e("Signature", "${AsTool.getAppSignature()?.get(0)?.toCharsString()}")

        initNetworkInfoLis()


//        if (temp != null) {
//          //一天内算法
//            if (temp <= 86400) {
//                var h = temp / 3600
//                var m = temp / 60
//                var s = temp % 60
//                when {
//                    h > 0 -> {
//                        itemBindding?.tvItemLocation?.text = "${h}小时前 ${bean?.dynamicRecord?.province} ${bean?.dynamicRecord?.city}"
//                    }
//                    m > 0 -> {
//                        itemBindding?.tvItemLocation?.text = "${m}分钟前 ${bean?.dynamicRecord?.province} ${bean?.dynamicRecord?.city}"
//                    }
//                    s > 0 -> {
//                        itemBindding?.tvItemLocation?.text = "${s}秒前 ${bean?.dynamicRecord?.province} ${bean?.dynamicRecord?.city}"
//                    }
//                }
//            } else {
//                val sdf = SimpleDateFormat("yyyy-MM-dd")
//                val nd = 1000 * 24 * 60 * 60.toLong() // 一天的毫秒数
//                val diff = sdf.parse(RxTimeUtils.milliseconds2String(System.currentTimeMillis())).time -
//                        sdf.parse(bean?.dynamicRecord?.createTime?.let { RxTimeUtils.milliseconds2String(it) }).time
//                val day = diff / nd
////                        LogUtils.e("时间差", "${diff / nd}")
//                if (day > 30 && (day / 30) < 12) {
//                    itemBindding?.tvItemLocation?.text = "${day / 30} 月前 " +
//                            "${bean?.dynamicRecord?.province}${bean?.dynamicRecord?.city}"
//                } else if (day > 365 || (day / 30) >= 12) {
//                    itemBindding?.tvItemLocation?.text = "${day / 365} 年前 " +
//                            "${bean?.dynamicRecord?.province}${bean?.dynamicRecord?.city}"
//                } else {
//                    itemBindding?.tvItemLocation?.text = "$day 天前 " +
//                            "${bean?.dynamicRecord?.province}${bean?.dynamicRecord?.city}"
//                }
//            }
//        }


        //删除文件
//        val file = File("${this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)}")
//        val files = file.listFiles() //获取SD卡指定路径下的文件或者文件夹
//        try {
//            for (i in files.indices) {
//                if (files[i].isFile) { //如果是文件直接删除
//                    val photoFile = File(files[i].path)
//                    LogUtils.e("Environment -->> ", photoFile.path)
//                    photoFile.delete()
//                } else {
//                    if (true) { //如果是文件夹再次迭代进里面找到指定文件路径
//                        val myfile = files[i].listFiles()
//                        for (d in myfile.indices) {
//                            val photoFile = File(myfile[d].path)
//                            photoFile.delete()
//                        }
//                    }
//                }
//            }
//        }catch (e: Exception){
//            e.printStackTrace()
//        }


        //使用需要导入"androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
//        // 直接开启一个协程，最常见的
//        lifecycleScope.launch {
//        }
//
//        // 在生命周期走到 Create 以后，开启此协程内容
//        lifecycleScope.launchWhenCreated {
//        }
//
//        // 在生命周期走到 Start 以后，开启此协程内容
//        lifecycleScope.launchWhenStarted {
//        }
//
//        // 在生命周期走到 Resume 以后，开启此协程内容
//        lifecycleScope.launchWhenResumed {
//        }

//        sb1.setOnSeekBarChangeListenerInline {
//            onProgressChanged { seekBar, progress, fromUser ->
//
//            }
//        }
//
//        sb1 setOnSeekBarChangeListenerInfix {
//            onProgressChanged { seekBar, progress, fromUser ->
//
//            }
//        }
//
//        sb2 setOnSeekBarChangeListenerInfix {
//            onProgressChanged { seekBar, progress, fromUser ->
//
//            }
//        }

//        ryView.setOnClickListener {
//            count++
//            when(count){
//                1->{
//                    ryView.start()
//                }
//                2->{
//                    ryView.pause()
//                }
//                3->{
//                    ryView.stop()
//                    count = 0
//                }
//            }
//        }
//        ryView2.setOnClickListener {
//            count1++
//            when(count1){
//                1->{
//                    ryView2.start()
//                }
//                2->{
//                    ryView2.pause()
//                }
//                3->{
//                    ryView2.stop()
//                    count1 = 0
//                }
//            }
//        }
//        ryView3.setOnClickListener {
//            count2++
//            when(count2){
//                1->{
//                    ryView3.start()
//                }
//                2->{
//                    ryView3.pause()
//                }
//                3->{
//                    ryView3.stop()
//                    count2 = 0
//                }
//            }
//        }
    }

    fun initNetworkInfoLis() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val connectivityManager =
                applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            connectivityManager.requestNetwork(NetworkRequest.Builder().build(), object : ConnectivityManager.NetworkCallback() {
                override fun onAvailable(network: Network?) {
                    super.onAvailable(network)
                    LogUtils.e("网络测试" , "onAvailable")
                }

                override fun onLost(network: Network?) {
                    super.onLost(network)
                    LogUtils.e("网络测试" , "onLost")
                }


            })
        }
    }
}
